local sk__jieying = fk.CreateSkill {

  name = "sk__jieying",

  tags = {},

}



sk__jieying:addEffect(fk.EventPhaseStart, {
  name = "sk__jieying",
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(sk__jieying.name) then
      return target == player and player:hasSkill(sk__jieying.name) and player.phase == Player.Draw and
          table.find(player.room:getOtherPlayers(player), function(p) return p:getMark("@sk__jieying") == 0 end)
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = table.map(table.filter(room:getOtherPlayers(player), function(p)
      return p:getMark("@sk__jieying") == 0
    end), Util.IdMapper)
    local to = room:askForChoosePlayers(player, targets, 1, 1, "#sk__jieying-invoke", sk__jieying.name)
    if #to > 0 then
      event:setCostData(self, to[1])
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(event:getCostData(self))
    room:addPlayerMark(to, "@sk__jieying", 3)
    data.phase_end = true
  end,
})
sk__jieying:addEffect(fk.BeforeDrawCard, {
  name = "sk__jieying",
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(sk__jieying.name) then
      if target:getMark("@sk__jieying") > 0 and not target.dead then
        return data.num > 0
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    return true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:doIndicate(player.id, { target.id })
    room:removePlayerMark(target, "@sk__jieying", 1)
    player:drawCards(data.num, sk__jieying.name)
    return true
  end,
})
sk__jieying:addEffect(fk.PreHpRecover, {
  name = "sk__jieying",
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(sk__jieying.name) then
      if target:getMark("@sk__jieying") > 0 and not target.dead then
        return data.num > 0
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    return true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:doIndicate(player.id, { target.id })
    room:removePlayerMark(target, "@sk__jieying", 1)
    if player:isWounded() then
      room:recover({
        who = player,
        num = math.min(data.num, player:getLostHp()),
        recoverBy = player,
        skillName = sk__jieying.name,
      })
    end
    data:preventRecover()
  end,
})
sk__jieying:addEffect(fk.BeforeMaxHpChanged, {
  name = "sk__jieying",
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(sk__jieying.name) then
      if target:getMark("@sk__jieying") > 0 and not target.dead then
        return data.num > 0
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    return true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:doIndicate(player.id, { target.id })
    room:removePlayerMark(target, "@sk__jieying", 1)
    room:changeMaxHp(player, data.num)
    data:preventMaxHpChange()
  end,
})
sk__jieying:addEffect(fk.EventAcquireSkill, {
  name = "sk__jieying",
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(sk__jieying.name) then
      if target:getMark("@sk__jieying") > 0 and not target.dead then
        return data.skill:isPlayerSkill(target) and data.skill.visible
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    return true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:doIndicate(player.id, { target.id })
    room:removePlayerMark(target, "@sk__jieying", 1)
    if not player:hasSkill(data.skill.name, true) then
      room:handleAddLoseSkills(player, data.skill.name, nil, true, false)
    end
    room:handleAddLoseSkills(target, "-" .. data.skill.name, nil, true, false)
  end,
})
sk__jieying:addEffect(fk.BeforeTurnStart, {
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill(sk__jieying.name) and target:getMark("@sk__jieying") > 0 and not target.dead and
        target:insideExtraTurn()
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke(sk__jieying.name)
    room:notifySkillInvoked(player, sk__jieying.name)
    room:doIndicate(player.id, { target.id })
    room:removePlayerMark(target, "@sk__jieying", 1)
    room:sendLog {
      type = "#GainAnExtraTurn",
      from = player.id
    }
    room.current = player
    room.tag["_extra_turn_count"] = room.tag["_extra_turn_count"] or {}
    local ex_tag = room.tag["_extra_turn_count"]
    ex_tag[-1] = sk__jieying.name
    room:sendLog { type = "$AppendSeparator" }
    if not player.faceup then
      player:turnOver()
      return true
    end
    room.logic:trigger(fk.BeforeTurnStart, player)
  end,
})

return sk__jieying
